/*
 * YuebaoTextView.cpp
 *
 *  Created on: 2011-3-17
 *      Author: 
 */

#include "YuebaoTextView.h"
#include "ProjectsDefines.h"
#include "BetterUserHBufC.h"
#include "MagazineDefines.h"

#define TEXT_LINE_HEIGHT ( _font->HeightInPixels() + 4 )
#define kMargin  2

fTextView* fTextView::NewLC( fEngine* aEngine )
{
    fTextView* self = new ( ELeave ) fTextView();
    CleanupStack::PushL( self );
    self->ConstructL( aEngine );
    return self;
}

fTextView* fTextView::NewL( fEngine* aEngine )
{
    fTextView* self = fTextView::NewLC( aEngine );
    CleanupStack::Pop(); // self;
    return self;
}

fTextView::fTextView()
{

}

fTextView::~fTextView()
{
    SAFE_DELETE( _content );
    SAFE_DELETE( _stringSpliter );
    SAFE_DELETE( _scrollbar );
}

void fTextView::ConstructL( fEngine* aEngine )
{
    BaseInit( aEngine );

    _font = fFont::get_device_font( kContentFontHeight, EFalse, EFalse );
    _text_color = 0x00;
    _text_align_h = EHCenter;
    _text_align_v = EVCenter;
    _showScrollbar = true;
    _scrollbar = fVScrollBar::NewL( iEngine );
}

void fTextView::SetRect( const TRect& aRect )
{
    SetCtlPos( aRect.iTl.iX, aRect.iTl.iY );
    SetCtlSize( aRect.Size().iWidth, aRect.Size().iHeight );
}

void fTextView::RenderFrame()
{
    _ASSERT_LOG( Width() != 0 );
    _ASSERT_LOG( Height() != 0 );

    //iGfx->DrawRect( Left( ), Top( ), Right( ), Bottom( ), 0xcdcdcd, 2 );
    if( _scrollbar && _scrollbar->IsVisible() && _showScrollbar )
    {
        _scrollbar->Render();
    }

    if( _stringSpliter )
    {

        const CDesC16Array& strings = _stringSpliter->GetTextArray();
        int count = strings.Count();

        if( count < 1 )
        {
            return;
        }
        TRect oldClipRect = iGfx->GetClipRect();
        iGfx->SetClipRect( TRect( Left(), Top(), Right(), Bottom() ) );
        int text_x = Left() + kMargin;
        int text_y = Top() + kMargin - _screen_offset_y;
        int text_y_end = Bottom() - kMargin;
        int line_height = TEXT_LINE_HEIGHT;

        int content_height = count * line_height;
        int title_text_y_offset = 0;

        int client_height = Height() - kMargin - kMargin;
        int client_width = Width() - kMargin - kMargin;

        if( _text_align_v == EVCenter )
        {
            title_text_y_offset = ( client_height - content_height ) >> 1;
        }
        else if( _text_align_v == EBottom )
        {
            title_text_y_offset = ( client_height - content_height );
        }
        else
        {
            title_text_y_offset = 0;
        }

        if( title_text_y_offset < 0 )
        {
            title_text_y_offset = 0;
        }

        text_y += title_text_y_offset;
        if( _text_align_h == ECenter )
        {
            for( int index = 0; index < count; index++ )
            {
                if( text_y + line_height > text_y_end )
                {
                    iGfx->SetClipRect( oldClipRect );
                    return;
                }
                const TDesC& string = strings[ index ];
                int text_width = _font->TextWidthInPixels( string );
                int text_x_offset = ( client_width - text_width ) >> 1;
                iGfx->DrawString( text_x + text_x_offset, text_y + kMargin, _text_color, _font, string );
                text_y += line_height;
            }
        }
        else if( _text_align_h == ERight )
        {
            for( int index = 0; index < count; index++ )
            {
                if( text_y + line_height > text_y_end )
                {
                    iGfx->SetClipRect( oldClipRect );//iGfx->iPen->SetClippingRect( TRect( 0, 0, iEngine->GetScreenWidth( ), iEngine->GetScreenHeight( ) ) );
                    return;
                }
                const TDesC& string = strings[ index ];
                int text_width = _font->TextWidthInPixels( string );
                int text_x_offset = ( client_width - text_width );
                iGfx->DrawString( text_x + text_x_offset, text_y + kMargin, _text_color, _font, string );
                text_y += line_height;
            }
        }
        else
        {
            //int text_x_offset = kMargin;
            for( int index = 0; index < count; index++ )
            {
                if( text_y + line_height > text_y_end )
                {
                    iGfx->SetClipRect( oldClipRect );
                    return;
                }
                const TDesC& string = strings[ index ];
                int text_width = _font->TextWidthInPixels( string );

                iGfx->DrawString( text_x, text_y + kMargin, _text_color, _font, string );
                text_y += line_height;
            }
        }

        iGfx->SetClipRect( oldClipRect );
        //iGfx->iPen->SetClippingRect( TRect( 0, 0, iEngine->GetScreenWidth( ), iEngine->GetScreenHeight( ) ) );
    }

}
void fTextView::PositonChanged( int /*x*/, int /*y*/ )
{
    _scrollbar->SetCtlPos( Right() - _scrollbar->Width(), Top() );
}
int fTextView::ContentHeight( )
{
    if( !_stringSpliter )
    {
        return 0;
    }
    const int lineHeight = TEXT_LINE_HEIGHT;
    CDesC16Array& list = _stringSpliter->GetTextArray( );

    return lineHeight * ( list.Count( ) + 1 ) + kMargin + kMargin;
}
 
void fTextView::SetContent( const TDesC& content )
{
    BetterUtility::Copy( _content, content );

    SAFE_DELETE(_stringSpliter );
    _stringSpliter = new StringSpliteUtility( );
    _total_height = 0;
    if( _stringSpliter && _content )
    {

        int client_width = Width( ) - kMargin - kMargin;
        _stringSpliter->SetFont( _font );
        if( _showScrollbar )
        {
            _stringSpliter->SetWidth( client_width - _scrollbar->Width( ) );
        }
        else
        {
            _stringSpliter->SetWidth( client_width );
        }
        _stringSpliter->SetText( _content->Des( ) );
        _stringSpliter->GetTextArray( );
        const int lineHeight = TEXT_LINE_HEIGHT;
        CDesC16Array& list = _stringSpliter->GetTextArray( );
        _total_height += lineHeight * list.Count( );
    }
    _screen_offset_y = 0;
    _total_height -= Height( );
    if( _total_height < 0 )
    {
        _total_height = 0;
    }
    else
    {
        if( _total_height % TEXT_LINE_HEIGHT != 0 )
        {
            _total_height += TEXT_LINE_HEIGHT;
        }
    }
    if( _total_height > 0 )
    {
        _scrollbar->SetCtlPos( Right( ) - _scrollbar->Width( ), Top( ) );
        _scrollbar->SetScrollHeight( Height( ) );

        _scrollbar->ResetValue( 0, _total_height + Height( ), 0 );
        _scrollbar->SetPageItemsCount( Height( ) );
        _scrollbar->MakeVisible( ETrue );
    }
    else
    {
        _total_height = 0;

        SAFE_DELETE(_stringSpliter );
        _stringSpliter = new StringSpliteUtility( );

        if( _stringSpliter && _content )
        {

            int client_width = Width( ) - kMargin - kMargin;
            _stringSpliter->SetFont( _font );
            _stringSpliter->SetWidth( client_width );
            _stringSpliter->SetText( _content->Des( ) );
            _stringSpliter->GetTextArray( );
            const int lineHeight = TEXT_LINE_HEIGHT;
            CDesC16Array& list = _stringSpliter->GetTextArray( );
            //_total_height += lineHeight * list.Count( );
        }

        _scrollbar->MakeVisible( EFalse );
    }
}

void fTextView::HandleKey( f_KEY_DES* aKeyDes )
{
    if( aKeyDes->type == f_KEYEVENT_POPED )
    {
        switch( aKeyDes->key )
        {
            case f_KEY_UP:
                {
                    const int lineHeight = TEXT_LINE_HEIGHT;
                    if( _screen_offset_y > 0 )
                    {
                        _screen_offset_y -= lineHeight;
                    }

                    if( _screen_offset_y < 0 )
                    {
                        _screen_offset_y = 0;
                    }
                    if( _total_height )
                    {
                        _scrollbar->SetCurrentValue( _screen_offset_y * ( _total_height + Height() ) / _total_height );
                    }
                }
                break;
            case f_KEY_DOWN:
                {
                    const int lineHeight = TEXT_LINE_HEIGHT;
                    if( _screen_offset_y < _total_height )
                    {
                        _screen_offset_y += lineHeight;
                    }

                    if( _screen_offset_y > _total_height )
                    {
                        _screen_offset_y = _total_height;
                    }
                    if( _total_height )
                    {
                        _scrollbar->SetCurrentValue( _screen_offset_y * ( _total_height + Height() ) / _total_height );
                    }
                }
                break;
            default:
                {

                }
                break;
        }
    }
}

void fTextView::SetFontHeight( int size )
{
    _font = fFont::get_device_font( size, EFalse, EFalse );
}

